from __future__ import absolute_import
import os
import os.path
import glob
import logging
import shutil
from os.path import isdir, join, exists
from forest.registry import register_tool
from forest.tool import Tool

logger = logging.getLogger('tools.system.copy')

def joinpaths(*args, **kwargs):
    path = os.path.sep.join(args)

    if kwargs.get("follow_symlinks"):
        return os.path.realpath(path)
    else:
        return path

@register_tool('system.copy')
def copy(src, dst, perms = None):
    dst = os.path.abspath(dst)

    if isdir(dst):
        dst = join(dst, os.path.basename(src))

    logger.info("Copy %s -> %s", src, dst)

    shutil.copy2(src, dst)
    if perms is not None:
        os.chmod(dst, perms)


    return dst
